﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace WebShop.Contracts
{
    [Serializable]
    public class ProductsCart
    {
        private readonly List<ProductInCart> products = new List<ProductInCart>();
        private bool editable;

        public bool Editable
        {
            get
            {
                return editable;
            }
            set
            {
                foreach (ProductInCart product in products)
                {
                    product.Editable = value;
                }
                editable = value;
            }
        }

        public IEnumerable<ProductInCart> Products
        {
            get { return products; }
        }

        public int[] ProductIDs
        {
            get { return products.Select(p => p.ProductID).ToArray(); }
        }

        public int Count { get { return products.Count; } }

        public ProductInCart GetProduct(int productID)
        {
            return products.FirstOrDefault(p => p.ProductID == productID);
        }

        public ProductInCart AddProduct(ProductInCart product)
        {
            ProductInCart p = products.SingleOrDefault(pr => pr.ProductID == product.ProductID);

            if (p == null)
            {
                products.Add(product);
            }
            else
            {
                p.Quantity += product.Quantity;
            }

            return p;
        }

        public void RemoveProduct(ProductInCart product)
        {
            ProductInCart p = products.SingleOrDefault(pr => pr.ProductID == product.ProductID);

            if (p == null)
            {
                return;
            }
            else
            {
                if (p.Quantity <= product.Quantity)
                {
                    products.Remove(p);
                }
                p.Quantity -= product.Quantity;
            }
        }

        public bool ContainsProduct(int ProductID)
        {
            return products.Contains(new ProductInCart(ProductID, 0));
        }

        public List<ProductInCart> FillProductsInfo(List<Product> productsInfo)
        {
            foreach (Product p in productsInfo)
            {
                foreach (ProductInCart item in products)
                {
                    if (item.ProductID == p.ProductID)
                    {
                        item.Product = p;
                    }
                }
            }
            return products;
        }

        public void UpdateProducts(List<ProductInCart> productsNew)
        {
            foreach (ProductInCart p in productsNew)
            {
                foreach (ProductInCart item in products)
                {
                    if (item.ProductID == p.ProductID)
                    {
                        item.Quantity = p.Quantity;
                    }
                }
            }
        }

        public void Clear()
        {
            products.Clear();
        }
    }

    [Serializable]
    public class ProductInCart : IComparable
    {
        [NonSerialized]
        private Product product;

        [NonSerialized]
        private bool editable = false;

        public Product Product
        {
            get { return product; }
            set { product = value; }
        }
        public bool Editable
        {
            get { return editable; }
            set { editable = value; }
        }
        public int ProductID { get; set; }
        public short Quantity { get; set; }

        public ProductInCart()
        {

        }

        public ProductInCart(int productID, short count)
        {
            ProductID = productID;
            Quantity = count;
        }

        public int CompareTo(object obj)
        {
            if (obj == null)
            {
                return 1;
            }

            return this.ProductID.CompareTo(obj);
        }
    }
}